package assignment0.visitor;

import java.util.HashMap;
import java.util.Map;

import assignment0.node.AssignNode;
import assignment0.node.ExpressionNode;
import assignment0.node.FactorNode;
import assignment0.node.IdentifierNode;
import assignment0.node.Node;
import assignment0.node.NumberNode;
import assignment0.node.TermNode;

public class Visitor {

	public Object visit(Node node) {
		if(node != null) {
			return node.visit(this);
		}
		return null;
	}

	public Object visitAssign(AssignNode n) {
		Map<String, Number> result = new HashMap<String, Number>();

		String left = (String) visit(n.left);
		Number right = (Number) visit(n.right);
		result.put(left, right);

		return result;
	}

	public Object visitExpression(ExpressionNode n) {
		Integer left = (Integer) visit(n.left);
		Integer right = (Integer) visit(n.right);

		if (n.operator != null) {
			if (n.operator.equals("+")) {
				return left + right;
			} else {
				return left - right;
			}
		}
		return left;
	}

	public Object visitTerm(TermNode n) {
		Integer left = (Integer) visit(n.left);
		Integer right = (Integer) visit(n.right);

		if (n.operator != null) {
			if (n.operator.equals("*")) {
				return left * right;
			} else {
				return left / right;
			}
		}
		return left;
	}

	public Object visitFactor(FactorNode n) {
		return visit(n.node);
	}

	public Object visitIdentifier(IdentifierNode n) {
		return n.value;
	}

	public Object visitNumber(NumberNode n) {
		return n.value;
	}
}